package pers.zc.activiti.file;

import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;

import java.io.*;
import java.util.*;

/**
 * @description 指定位置读取文件
 * @Author Lin FuYuan
 * @date 2020/4/28
 */
public class ReadFile {

    public static void read() {
        long from = 4 + 1;//从该字节开始读，自己测注意中文是两个字节
        try {
            File file = new File("C:\\Users\\Administrator\\Desktop\\text\\test.txt");
            FileInputStream bis = new FileInputStream(file);
            //文件指向前一字节
            bis.skip(from - 1);
            //指定文件位置读取的文件流
            InputStream sbs = new BufferedInputStream(bis);
            //存入文件，以便检测
            File file1 = new File("C:\\Users\\Administrator\\Desktop\\text\\test2.txt");
            OutputStream os = null;
            try {
                os = new FileOutputStream(file1);
                byte buffer[] = new byte[4 * 1024];
                int len = 0;
                while ((len = sbs.read(buffer)) != -1)//
                {
                    os.write(buffer, 0, len);
                }
                os.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }


    public static void createFile() throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\text\\test.txt");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        for (int i = 0; i < 1000000; i++) {
            String uuid = UUID.randomUUID().toString();
            writer.write(i + "    uuid:" + uuid);
            writer.newLine();
        }
        writer.close();
    }


    public static void createConf() throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\text\\demo.yml");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        List<Map<String, String>> listMap = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("key:" + i, "value" + i);
            listMap.add(map);
        }
        for (Map<String, String> s : listMap) {
            s.forEach((k, v) -> {
                try {
//                    writer.write(
//                            "<parent>\n" +
//                                    "        <name>" + k + "</name>\n" +
//                                    "        <value>" + v + "</value>\n" +
//                                    "</parent>");
                    writer.write(k+": "+v);
                    writer.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
        writer.close();
    }

    public static void main(String[] args) throws IOException {
//        createConf();
//        createFile();
//        readLineNum();
//        readLineNum2();
    }


    public static void readLineNum() throws IOException {
        Long start = System.currentTimeMillis();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("C:\\Users\\Administrator\\Desktop\\text\\test.txt")));
        StringBuffer buff = new StringBuffer();
        String line = bufferedReader.readLine();
        Long n = 0L;
        while (StringUtils.isNotBlank(line)) {

            if (n >= 500000L) {
                buff.append(line);
            }
            n++;
            line = bufferedReader.readLine();
        }
        Long read = System.currentTimeMillis() - start;
        System.out.println(read + "   读取");
        //写入到另一个文件
        File file = new File("C:\\Users\\Administrator\\Desktop\\text\\test2.txt");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(buff.toString());
        Assert.assertNull(writer);
        writer.close();
        System.out.println();
        Long write = System.currentTimeMillis() - start;
        System.out.println(write - read + "  写入");
    }

    public static void readLineNum2() throws IOException {
        Long start = System.currentTimeMillis();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("C:\\Users\\Administrator\\Desktop\\text\\test3.txt")));
        StringBuffer buff = new StringBuffer();
        String line = bufferedReader.readLine();
        Long n = 0L;
        while (StringUtils.isNotBlank(line)) {
            buff.append(line);
            n++;
            line = bufferedReader.readLine();
        }
        Long read = System.currentTimeMillis() - start;
        System.out.println(read + "   读取");
        //写入到另一个文件
        File file = new File("C:\\Users\\Administrator\\Desktop\\text\\test4.txt");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(buff.toString());
        writer.close();
        System.out.println();
        Long write = System.currentTimeMillis() - start;
        System.out.println(write - read + "  写入");
    }
}
